package com.dingreading.cloud.soms.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dingreading.cloud.common.dto.CorpTeacherDto;
import com.dingreading.cloud.soms.entity.Staff;
import com.dingreading.cloud.soms.entity.table.StaffTableDef;
import com.dingreading.cloud.soms.mapper.StaffMapper;
import com.dingreading.cloud.soms.service.StaffService;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

import static com.dingreading.cloud.soms.entity.table.StaffRolesTableDef.staffRoles;
import static com.dingreading.cloud.soms.entity.table.StoresTableDef.stores;


/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class StaffServiceImpl extends BaseServiceImpl<StaffMapper, Staff, StaffTableDef> implements StaffService {

    @Override
    protected StaffTableDef getTable() {
        return StaffTableDef.staff;
    }

    @Resource
    private StaffMapper staffMapper;

    @Override
    public List<CorpTeacherDto> getCorpTeachers(String storeUid, String keyword, String staffUid, Integer enabled) {
        return staffMapper.getCorpTeachers(storeUid, keyword, staffUid, enabled);
    }

    @Override
    public Staff getByUid(String uid) {
        return getMapper().selectOneByCondition(table.uid.eq(uid));
    }

    @Override
    public List<JSONObject> listByStoreStaff(String storeUid, Integer enabled, String keyword, Integer marketer) {
        List<JSONObject> list = staffMapper.listByStoreStaff(storeUid, enabled, keyword, marketer);
        if (CollectionUtils.isNotEmpty(list)) {
            for (JSONObject jsonObject : list) {
                String fullName = jsonObject.getString("fullName");
                String nickName = jsonObject.getString("nickName");
                jsonObject.put("nickName", StringUtils.isNotBlank(nickName) ? nickName : fullName);
            }
        }
        return list;
    }

    @Override
    public Staff getByUsername(String username) {
        return getMapper().selectOneByCondition(table.username.eq(username));
    }

    @Override
    public List<Staff> listByCorpMarketer(String corpUid, Integer enabled, String keyword) {
        QueryWrapper storeWrapper = QueryWrapper.create()
                .select(stores.uid)
                .from(stores)
                .where(stores.corpUid.eq(corpUid));

        List<String> storeUids = getMapper().selectObjectListByQueryAs(storeWrapper, String.class);

        QueryWrapper wrapper = QueryWrapper.create().select(QueryMethods.distinct(staffRoles.staffUid))
                .from(staffRoles)
                .where(
                        staffRoles.storeUid.in(storeUids)
                                .and(staffRoles.roles.like(",22,"))
                                .and(staffRoles.inStoreService.eq(1))
                );

        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(table.corpUid, table.fullName, table.nickName, table.uid,
                        table.username, table.pubPhone, table.priPhone, table.enabled, table.lastLoginTime)
                .from(table)
                .where(table.uid.in(wrapper))
                .orderBy(table.id.desc());

        return getMapper().selectListByQuery(queryWrapper);
    }

    @Override
    public List<Staff> listByCorpMarketerV2(String staffUid, Integer enabled, String keyword) {
        QueryWrapper staffRoleWrapper = QueryWrapper.create()
                .select(staffRoles.storeUid)
                .from(staffRoles)
                .where(staffRoles.staffUid.eq(staffUid));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(QueryMethods.distinct(staffRoles.staffUid))
                .from(staffRoles)
                .where(
                        staffRoles.storeUid.in(staffRoleWrapper)
                                .and(staffRoles.roles.like(",22,"))
                                .and(staffRoles.inStoreService.eq(1))
                );

        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(table.corpUid, table.fullName, table.nickName, table.uid,
                        table.username, table.pubPhone, table.priPhone, table.enabled, table.lastLoginTime
                )
                .from(table)
                .where(table.uid.in(wrapper))
                .orderBy(table.id.desc());

        return getMapper().selectListByQuery(queryWrapper);
    }

    @Override
    public Staff getByStoreUidAndName(String storeUid, String fullName) {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.allColumns)
                .from(table)
                .leftJoin(staffRoles).on(table.uid.eq(staffRoles.staffUid))
                .where(table.fullName.eq(fullName).and(staffRoles.storeUid.eq(storeUid)));
        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public List<Staff> listByStoreUidAndMstUidIn(String storeUid, Set<String> mstUidList) {
        QueryCondition condition = table.currentStoreUid.eq(storeUid)
                .and(table.uid.in(mstUidList));
        return getMapper().selectListByCondition(condition);
    }

    @Override
    public Staff getLastUsername(String corpUid) {
        QueryWrapper wrapper = QueryWrapper.create()
                .where(table.username.likeLeft(corpUid))
                .orderBy(table.id.desc());

        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public Staff getByStaffUid(String corpUid, String staffUid) {
        QueryCondition condition = QueryCondition.createEmpty()
                .and(table.corpUid.eq(corpUid))
                .and(table.uid.eq(staffUid));
        return getMapper().selectOneByCondition(condition);
    }

}
